﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApp.localhost;

namespace WindowsFormsApp
{
    public partial class FrmTakeOrder : Form
    {
        public FrmTakeOrder()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            localhost.WebService client = new localhost.WebService();

            client.CookieContainer = CookieContainer.Data;

            if (textBox1mingzi.Text == null)
            {
                MessageBox.Show("请输入订单名字！如：今天点XXXX饭馆的饭");
            }
            else if(dateTimePicker1time.Text == null)
            {
                MessageBox.Show("请选择订餐结束时间！");
            }
            else if (comboBox1canting.Text == null)
            {
                MessageBox.Show("请选择餐厅！");
            }
            else
            {
                Order a = new Order();
                
                a.ReMark = this.textBox1mingzi.Text;

                a.AddTime = this.dateTimePicker1time.Value;

                KeyValuePair<int,string> item = (KeyValuePair<int,string>)this.comboBox1canting.SelectedItem;

                a.DiningRoomId = item.Key;

                try
                {
                    client.InsetOrder(a);

                    MessageBox.Show("添加成功！");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void FrmTakeOrder_Load(object sender, EventArgs e)
        {
            localhost.WebService client = new WebService();

            client.CookieContainer = CookieContainer.Data;

            var dd = client.GetDiningRoom();

            foreach (var d in dd)
            {
                comboBox1canting.Items.Add(new KeyValuePair<int, string>(d.Id, d.Name));
            }

            if (comboBox1canting.Items != null && comboBox1canting.Items.Count > 0)
            {
                comboBox1canting.SelectedIndex = 0;
            }
        }

        private void comboBox1canting_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.textBox1mingzi.Text = ((KeyValuePair<int, string>)comboBox1canting.SelectedItem).Value;
        }


    }
}
